package com.xy.p2p.action.front;



import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Tfileaveamessage;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.service.TfileaveamessageService;



@Controller
public class TfileaveamessageAction{
	private static final Logger log = LoggerFactory.getLogger(TfileaveamessageAction.class);
	public TfileaveamessageAction() {
	}
 
 
 	/**
	 * 融资标的留言表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfileaveamessageAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tfileaveamessage", new Tfileaveamessage());
        setDict(model);
		return "tfileaveamessageEdit";
	}

	/**
	 * 融资标的留言表新增保存
	 * 
	 * @param tfileaveamessage
	 * @param request
	 * @param model
	 * @return
	 */
//	@RequestMapping("/o_tfileaveamessageSave.do")
//	public String save(Tfileaveamessage tfileaveamessage, HttpServletRequest request, ModelMap model) {
//		WebErrors errors = validateSave(tfileaveamessage, request);
//		if (errors.hasErrors()) {
//			return errors.showErrorPage(model);
//		}
//		tfileaveamessage = tfileaveamessageService.save(tfileaveamessage);
//		return "redirect:v_tfileaveamessageList.do";
//	}
	
	/**
	 * ajax提交
	 * @param tfileaveamessage
	 * @param request
	 * @param model
	 */
	@RequestMapping("/member/o_tfileaveamessageSave.jspx")
	public void save(Tfileaveamessage tfileaveamessage, HttpServletRequest request, ModelMap model, HttpServletResponse response) {
		tfileaveamessage.setTmember((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		tfileaveamessage.setLmdate(new Date());
		try {
			response.setContentType("text/Xml;charset=gbk");
			PrintWriter out = response.getWriter();
			if(null!=tfileaveamessageService.save(tfileaveamessage)){
				out.print(true);
			}else{
				out.print(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 融资标的留言表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tfileaveamessageDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tfileaveamessageService.remove(id);
			}
		}
		return "redirect:v_tfileaveamessageList.do";
	}

	/**
	 * 融资标的留言表编辑页面
	 * 
	 * @param filmid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfileaveamessageEdit.do")
	public String edit(long filmid, HttpServletRequest request, ModelMap model) {
		Tfileaveamessage tfileaveamessage = tfileaveamessageService.get(filmid);
		model.addAttribute("tfileaveamessage", tfileaveamessage);
        setDict(model);
		return "tfileaveamessageEdit";
	}

	/**
	 * 融资标的留言表详情
	 * 
	 * @param filmid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfileaveamessageDetail.do")
	public String detail(long filmid, HttpServletRequest request, ModelMap model) {
		Tfileaveamessage tfileaveamessage = tfileaveamessageService.get(filmid);
		model.addAttribute("tfileaveamessage", tfileaveamessage);
        setDict(model);
		return "tfileaveamessageView";
	}

	/**
	 * 融资标的留言表查询（分页）
	 * 
	 * @param tfileaveamessage
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfileaveamessageList.do")
	public String listPage(Tfileaveamessage tfileaveamessage, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tfileaveamessageService.getTfileaveamessageByPage(toMap(tfileaveamessage),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tfileaveamessageList";
	}

	/**
	 * 融资标的留言表查询
	 * 
	 * @param tfileaveamessage
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tfileaveamessageList1.do")
	public String list(Tfileaveamessage tfileaveamessage, HttpServletRequest request, ModelMap model) {
		List<Tfileaveamessage> list = tfileaveamessageService.getTfileaveamessage(toMap(tfileaveamessage));
		model.addAttribute("list", list);
        setDict(model);
		return "tfileaveamessageList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tfileaveamessage
	 * @return
	 */	private Map<String,Object> toMap(Tfileaveamessage tfileaveamessage){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tfileaveamessage.getFilmid()!=null&&!tfileaveamessage.getFilmid().equals("")){
			map.put("filmid", tfileaveamessage.getFilmid());
		}
		if(tfileaveamessage.getTfinanceinfo()!=null){
			map.put("Tfinanceinfo", tfileaveamessage.getTfinanceinfo());
		}
		if(tfileaveamessage.getTmember()!=null){
			map.put("Tmember", tfileaveamessage.getTmember());
		}
		if(tfileaveamessage.getLmconten()!=null&&!tfileaveamessage.getLmconten().equals("")){
			map.put("lmconten", tfileaveamessage.getLmconten());
		}
		if(tfileaveamessage.getLmdate()!=null&&!tfileaveamessage.getLmdate().equals("")){
			map.put("lmdate", tfileaveamessage.getLmdate());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tfileaveamessage tfileaveamessage, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TfileaveamessageService tfileaveamessageService;   
}